package com.banmao.algorithm.No101To200.No144;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 *  迭代法 前序遍历 二叉树
 */
public class Solution2 {

    public List<Integer> preorderTraversal(TreeNode root) {

        Stack<TreeNode> stack = new Stack<>();
        ArrayList<Integer> res = new ArrayList<>();

        if(root == null) {
            return res;
        }

        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode pop = stack.pop();
            res.add(pop.val);

            if(pop.right != null) {
                stack.push(pop.right);
            }

            if(pop.left != null) {
                stack.push(pop.left);
            }

        }

        return res;

    }

}
